<template>
  <div>
    <div class="tl-form-line-title sub-title">
      <span style="font-size:14px;color: #606266;">关联任务：</span>
      <div class="toolbar">
        <el-button type="text" size="medium" v-if="!disabled && milepost.planfinishtime !== undefined && milepost.planfinishtime !== null" icon="el-icon-circle-plus-outline" @click="openAddDialog('editForm')">新增任务</el-button>
      </div>
    </div>
    <el-table class="tl-table" ref="mainTable" :default-expand-all="true" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
      <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
      <el-table-column prop="taskname" label="任务名称" min-width="180px" sortable="custom" sort-by="taskname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="responsible" label="责任人" width="85px" sortable="custom" sort-by="responsible" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="planstartdate" label="计划开始日期" width="120px" sortable="custom" sort-by="planstartdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="planenddate" label="计划完成日期" width="120px" sortable="custom" sort-by="planenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column prop="taskdetail" label="任务内容" width="300px" sortable="custom" sort-by="taskdetail" align="center" header-align="center" :show-overflow-tooltip="true"> </el-table-column>
      <el-table-column v-if="!disabled" label="操作" width="100px" v-slot="scope" fixed="right">
        <el-dropdown trigger="hover">
          <el-button type="text" icon="el-icon-s-operation" style="margin-left: 10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
          <el-dropdown-menu>
            <el-dropdown-item type="text" icon="el-icon-edit" @click.native="openEditDialog('editForm', scope.row.id)" :disabled="!doRowEdit(scope.row)">编辑</el-dropdown-item>
            <el-dropdown-item type="text" icon="el-icon-delete" @click.native="doDelete(scope.row.id)" v-show="showDeleteButton(scope.row)">删除</el-dropdown-item>
            <el-dropdown-item type="text" icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
          <!-- <el-button @click="openViewDialog('editFeedbackList', scope.row.id)" :disabled="scope.row.taskstate === '0'">
            历史进度
          </el-button> -->
        </el-table-column>
      </el-table>
      <tl-edit ref="editForm" @refresh="doRefresh" :milepost="milepost" :milepostname="milepostname" :milepostcode="milepostcode" :planstate="planstate" :projectinfo="projectinfo"></tl-edit>
    </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import { checkPermission } from '@tapui/utils/permission'
import { validateNull } from '@tapui/utils/validate'
import tlEdit from './editTask'
import tlProject from '../gpmProjectTask/editProjectRef'
import tlProjecttaskTemplate from '../gpmProjectTask/editProjectTaskTemplateRef'
import tlEditFeedbacklist from '../gpmProjectTaskFeedback/refList'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
import { filterObjectNull, rmbFormat } from '@tapui/utils/util'
export default {
  name: 'sdProjectTask',
  mixins: [mixin_list],
  components: { tlEdit, tlProject, tlProjecttaskTemplate, tlEditFeedbacklist },
  data() {
    return {
      description: '项目任务-列表查询',
      urlPrefix: '/sd/projectTask/',
      url: {
        queryCodeList: '/sd/projectTask/dataCodeList',
        deliveryBatch: '/sd/projectTask/submit'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'milepostname',
        sord: 'asc'
      },
      // 分页参数
      pageInfo: {
        currentPage: 1,
        pageSize: 2000
      },
      canDelete: true,
      isState: 0,
      canEdit: false,
      reduceHeight: 325
    }
  },
  props: {
    projectinfo: {},
    milepostcode: {
      type: [Number, String],
      default: '-1'
    },
    milepostname: {
      type: [Number, String],
      default: ''
    },
    planstate: {
      type: [Number, String],
      default: ''
    },
    index: {
      type: [Number, String],
      default: 0
    },
    disabled: {
      type: Boolean,
      default: false
    },
    milepost: {}
  },
  watch: {
    milepost: function(oldV, newV) {
      this.doQuery()
    }
  },
  computed: {
    // 里程碑下任务列表的删除按钮显示/隐藏权限
    showDeleteButton() {
      return (row) => {
        if (row.taskstate === '0') {
          return true
        }
        return false
      }
    }
  },
  methods: {
    beforeOpenAddDialog(editForm, rowData) {
      this.$parent.$parent.$parent.$parent.saveMilepost(this.index)
      return true
    },
    beforeLoadData() {
      this.queryParam.qry_milepostcode = this.milepostcode
      this.queryParam.qry_milepostid = this.milepost.id
      return true
    },
    /** 批量删除数据 */
    doDeleteBatch() {
      if (!this.url.delete) {
        this.$tap.alert('请设置delete地址!', 'warning')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$tap.alert('请选择记录！', 'warning')
        return
      } else {
        var deleteBybusinessManager = ''
        this.isState = 0
        for (var a = 0; a < this.selectionRows.length; a++) {
          if (this.selectionRows[a].state === 9 && this.selectionRows[a].taskstate === '0') {
            // state=9并且taskstate=0,表示任务已下发但是未执行
            this.isState = 1
          }
          if (this.selectionRows[a].state === 9 && this.selectionRows[a].taskstate === '1' && this.$store.getters.projectroles[0] === 'businessManager') {
            // state=9并且taskstate=1,表示任务已下发并且执行
            this.isState = 2
          }
        }
        var ids = ''
        for (var index = 0; index < this.selectedRowKeys.length; index++) {
          if (index === this.selectedRowKeys.length - 1) {
            ids += this.selectedRowKeys[index]
          } else {
            ids += this.selectedRowKeys[index] + ','
          }
        }
        if (this.isState === 1) {
          this.$tap
            .confirm('删除的任务包含已下发但未执行的任务，确定删除吗？')
            .then((_) => {
              if (!this.beforeDoDelete(ids)) {
                return
              }
              deleteAction(this.url.deleteBatch, { ids: ids }).then((res) => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.loadData({})
                  this.afterDoDelete(ids)
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
              })
            })
            .catch((_) => { })
        } else if (this.isState === 2) {
          this.$tap
            .confirm('删除的任务包含已下发并且已执行的任务，确定删除吗？')
            .then((_) => {
              if (!this.beforeDoDelete(ids)) {
                return
              }
              deleteBybusinessManager = deleteBybusinessManager.slice(0, -1)
              deleteAction(this.url.deleteBatch, { ids: ids }).then((res) => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.loadData({})
                  this.afterDoDelete(ids)
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
              })
              // 当经营管理部删除已下发并已执行的任务时，需要删除对应的任务的任务反馈
              deleteAction('/sd/projectTaskFeedback/deletecasetask', { ids: ids }).then((res) => {
                if (res.success) {
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
              })
            })
            .catch((_) => { })
        } else {
          this.$tap
            .confirm('确定删除数据吗？')
            .then((_) => {
              if (!this.beforeDoDelete(ids)) {
                return
              }
              deleteAction(this.url.deleteBatch, { ids: ids }).then((res) => {
                if (res.success) {
                  this.$tap.message(res.message, 'success')
                  this.loadData({})
                  this.afterDoDelete(ids)
                } else {
                  this.$tap.alert(res.message, 'warning')
                }
              })
            })
            .catch((_) => { })
        }
      }
    },
    deliveryBatch() {
      if (this.selectedRowKeys.length <= 0) {
        this.$tap.alert('请选择数据', 'warning')
        return
      } else {
        this.$tap
          .confirm('确定批量下发吗？')
          .then((_) => {
            putAction('/sd/projectTask/submit', { modelArray: this.selectionRows }).then((res) => {
              if (res.success) {
                this.$tap.message(res.message, 'success')
                this.loadData({})
              } else {
                this.$tap.alert(res.message, 'warning')
              }
            })
          })
          .catch((_) => { })
      }
    },
    /** 配置编辑权限 */
    doRowEdit(row, column, event) {
      let canEdit = false
      // 当任务的状态为未开始或进行中时，支持策划变更时修改任务的状态
      if (row.taskstate === '0' || row.taskstate === '1') {
        canEdit = true
      }
      if (!this.disabled && canEdit) {
        return true
      } else {
        return false
      }
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      let canEdit = false
      // 经营管理部人员可以编辑已下发并且已执行的任务
      if (this.$store.getters.projectroles[0] === 'businessManager' && row.taskstate === '1') {
        canEdit = true
      }
      if (this.$store.getters.projectroles[0] === 'businessManager' && row.taskstate === '0') {
        canEdit = true
      }
      // 项目经理可以编辑已下发但是未执行的任务
      if (this.$store.getters.projectroles[0] === 'pm' && row.taskstate === '0') {
        canEdit = true
      }
      // 判断双击打开是查看还是编辑
      if (!this.disabled && canEdit) {
        this.rowDbClickActionType = 'edit'
      } else {
        this.rowDbClickActionType = 'view'
      }
      if (this.rowDbClickActionType === 'edit') {
        // 判断是否有权限
        if (this.editPermission.length > 0) {
          if (checkPermission(this.editPermission)) {
            this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
          } else {
            this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
          }
        } else {
          this.openEditDialog('editForm', row[this.$refs.mainTable.rowKey])
        }
      } else if (this.rowDbClickActionType === 'view') {
        this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
      }
    },
    /** 获取查询参数 */
    getQueryParams() {
      let param = {}
      if (this.superQueryParams) {
        param['superQueryParams'] = encodeURI(this.superQueryParams)
      }
      Object.assign(param, this.queryParam, this.sorterInfo, this.filtersInfo)
      param.currentPage = this.pageInfo.currentPage
      param.pageSize = this.pageInfo.pageSize
      param.qry_projectcode = this.projectinfo.projectcode
      this.queryParam.qry_milepostcode = ''
      return filterObjectNull(param)
    },
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
     if (row.state !== 0 && row.state !== 2) {
        if (this.$store.getters.projectroles[0] === 'businessManager' && row.taskstate === '1') {
          return true
        }
        if (this.$store.getters.projectroles[0] === 'businessManager' && row.taskstate === '0') {
          return true
        }
        if (this.$store.getters.projectroles[0] === 'pm' && row.taskstate === '0') {
          return true
        }
        return false
      } else {
        return true
      }
    },
    /** 格式化任务进度 */
    percentageFormat(row, column, cellValue, index) {
      let taskScheduleValue = row[column.property]
      if (typeof taskScheduleValue === 'undefined' || taskScheduleValue === null || taskScheduleValue === '') {
        return '0%'
      }
      taskScheduleValue = taskScheduleValue + '%'
      return taskScheduleValue
    }
  }
}
</script>

<style scoped >
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand /deep/ .el-form-item__label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }
</style>

